//
//  BaseMTInfoVerificationCell.swift
//  MT
//
//  Created by M on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class BaseMTInfoVerificationCell: UITableViewCell {
    
    var type: UserProfileInfos? {
        didSet {
            guard let type = type else { return }
            titleLabel.text = type.title
            textField.isEnabled = type.editable
            textField.attributedPlaceholder = type.placeholder
//            underline.isHidden = type == .portrait
            let shouldAccessoryHidden: Bool = type.inputType != .picker && type.inputType != .tag
            accessoryImageIcon.isHidden = shouldAccessoryHidden
            accessoryImageIcon.snp.updateConstraints { (make) in
                make.width.equalTo(shouldAccessoryHidden ? 0 : 14)
            }
        }
    }
    
    /// 0：短、1：宽
    private var _screenModel: Int = 0
    var screenMode: Int {
        set {
            if newValue == _screenModel {
                return
            }
            _screenModel = newValue
            titleLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(contentView).offset(_screenModel == 0 ? 52 : 16)
                make.centerY.equalTo(contentView)
                make.width.equalTo(80)
                make.height.equalTo(22)
            }
            
            accessoryImageIcon.snp.remakeConstraints { (make) in
                make.right.equalTo(contentView).offset(_screenModel == 0 ? -52 : -16)
                make.centerY.equalTo(titleLabel)
                make.width.height.equalTo(14)
            }
            
            underline.snp.remakeConstraints { (make) in
                if _screenModel == 0 {
                    make.left.equalTo(titleLabel)
                    make.right.equalTo(accessoryImageIcon).offset(13)
                } else {
                    make.left.right.equalTo(contentView)
                }
                make.bottom.equalTo(contentView)
                make.height.equalTo(0.5)
            }
        }
        get {
            return _screenModel
        }
    }
    
    ///
    var textFieldTextAlignment: NSTextAlignment = .left {
        didSet {
            textField.textAlignment = textFieldTextAlignment
        }
    }
    
    /// 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCSemiboldFont(14)
        label.textColor = UIColor(hex:0x1d1a1a)
        label.textAlignment = .left
        return label
    }()

    /// 输入框
    lazy var textField: UITextField = {
        let tf = UITextField()
        tf.textColor = UIColor(hex:0x1d1a1a)
        tf.font = PingFangSCMediumFont(14)
        tf.textAlignment = .left
//        tf.keyboardAppearance = .dark
        
        let tool = UIToolbar(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        tool.barStyle = .black
        
        let spacing = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        btn.setTitle("完成", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(14)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(infoVerificationCell(didClickTextFieldToolBar:)), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn)
        tool.items = [spacing, item1]
        tf.inputAccessoryView = tool
        
        return tf
    }()
    
    /// 下划线
    lazy var underline: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xe7e7e7)
        return view
    }()
    
    /// accessory image icon
    lazy var accessoryImageIcon: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "icon_right_gary")
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension BaseMTInfoVerificationCell {
    
    private func initializeUI() {
        selectionStyle = .none
        contentView.backgroundColor = UIColor.theme.background
        contentView.addSubview(titleLabel)
        contentView.addSubview(textField)
        contentView.addSubview(underline)
        contentView.addSubview(accessoryImageIcon)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(10)
            make.top.equalTo(contentView).offset(22)
            make.width.equalTo(60)
            make.height.equalTo(22)
        }
        
        accessoryImageIcon.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-52)
            make.centerY.equalTo(titleLabel)
            make.width.height.equalTo(14)
        }
        
        textField.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(14)
//            make.right.equalTo(accessoryImageIcon.snp.left).offset(-10)
            make.right.equalTo(contentView).offset(-40)
            make.height.equalTo(titleLabel)
        }
        
        underline.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(20)
            make.right.equalTo(contentView).offset(-20)
            make.bottom.equalTo(contentView)
            make.height.equalTo(0.5)
        }
        
    }
    
}

// MARK: ------------------------------------ 事件方法 ----------------------------------------
@objc
extension BaseMTInfoVerificationCell {
    
    private func infoVerificationCell(didClickTextFieldToolBar sender: UIBarButtonItem) {
        if textField.isFirstResponder {
            textField.resignFirstResponder()
        }
    }
    
}


